package UI_vente;

import GestionBilleterie.Client;
import GestionBilleterie.Reservation;
import GestionBilleterie.Tarif;
import GestionBilleterie.Vendeur;
import GestionPlannification.Representation;
import javax.swing.JOptionPane;
import outils.VerifChamps;

/**
 *
 * @author Elsa Ferrare, Alexandre Becquet
 */
public class Panel_Validation extends javax.swing.JPanel {
    private Representation maRepresentation;
    private Tarif monTarif;
    private int nbPlaceReserv;
    private Frame_vente maFrame;
    private Client monClient;
    private Vendeur monVendeur;

    /** Creates new form Panel_Validation */
    public Panel_Validation(Frame_vente uneFrame,Representation r, Tarif t, Vendeur unVendeur, int nbPlace) {
        maFrame=uneFrame;
        maRepresentation=r;
        monTarif=t;
        nbPlaceReserv=nbPlace;
        monVendeur=unVendeur;

        initComponents();

        if(this.monVendeur==null){
            this.jTextFieldEmail.setEditable(false);
            this.jTextFieldNom.setEditable(false);
            this.jTextFieldPrenom.setEditable(false);
            this.jTextFieldTelephone.setEditable(false);
            this.jButtonMAJClient.setVisible(false);
            this.jButtonNewClient.setVisible(false);
        }

        this.jTextFieldNumClient.setInputVerifier(new VerifChamps(VerifChamps.NOMBRE));
        this.jTextFieldEmail.setInputVerifier(new VerifChamps(VerifChamps.EMAIL));
        this.jTextFieldTelephone.setInputVerifier(new VerifChamps(VerifChamps.TELEPHONE));
        this.jTextFieldNom.setInputVerifier(new VerifChamps(VerifChamps.TEXTCHAR));
        this.jTextFieldPrenom.setInputVerifier(new VerifChamps(VerifChamps.TEXTCHAR));

        this.jLabelPiece.setText("Pièce : "+this.maRepresentation.getPiece().getTitre());
        this.jLabelRepresentation.setText("Représentation : "+this.maRepresentation.toString());
        this.jLabelTarif.setText("Tarif : "+this.monTarif.toString());
        this.jLabelNbPlaces.setText("Nombre de places : "+this.nbPlaceReserv);
        this.jLabelTotal.setText("Total : "+this.nbPlaceReserv*this.monTarif.getPrix()+"€");   
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelValidation = new javax.swing.JLabel();
        jLabelPiece = new javax.swing.JLabel();
        jLabelRepresentation = new javax.swing.JLabel();
        jLabelNbPlaces = new javax.swing.JLabel();
        jLabelTarif = new javax.swing.JLabel();
        jLabelTotal = new javax.swing.JLabel();
        jButtonValider = new javax.swing.JButton();
        jButtonAnnuler = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldTelephone = new javax.swing.JTextField();
        jTextFieldEmail = new javax.swing.JTextField();
        jTextFieldNom = new javax.swing.JTextField();
        jTextFieldPrenom = new javax.swing.JTextField();
        jButtonRechercheClient = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldNumClient = new javax.swing.JTextField();
        jButtonMAJClient = new javax.swing.JButton();
        jButtonNewClient = new javax.swing.JButton();

        setOpaque(false);

        jLabelValidation.setText("Validation réservation");

        jLabelPiece.setText("Pièce :");

        jLabelRepresentation.setText("Representation :");

        jLabelNbPlaces.setText("Nombre de places :");

        jLabelTarif.setText("Tarif :");

        jLabelTotal.setText("Total :");

        jButtonValider.setText("Valider commande");
        jButtonValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonValiderActionPerformed(evt);
            }
        });

        jButtonAnnuler.setText("Annuler la commande");
        jButtonAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAnnulerActionPerformed(evt);
            }
        });

        jLabel1.setText("Nom : ");

        jLabel2.setText("Client");

        jLabel3.setText("Prénom :");

        jLabel4.setText("E-mail :");

        jLabel5.setText("Telephone :");

        jTextFieldTelephone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldTelephoneActionPerformed(evt);
            }
        });

        jButtonRechercheClient.setText("Recherche Client");
        jButtonRechercheClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRechercheClientActionPerformed(evt);
            }
        });

        jLabel6.setText("N° Client :");

        jButtonMAJClient.setText("Mise à Jour Client");
        jButtonMAJClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMAJClientActionPerformed(evt);
            }
        });

        jButtonNewClient.setText("Enregistrer Nouveau Client");
        jButtonNewClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewClientActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(jButtonValider)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonAnnuler))
                    .addComponent(jLabelValidation)
                    .addComponent(jLabelPiece)
                    .addComponent(jLabelRepresentation)
                    .addComponent(jLabelNbPlaces)
                    .addComponent(jLabelTarif)
                    .addComponent(jLabelTotal))
                .addContainerGap(421, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 797, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel1)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTextFieldNom)
                            .addComponent(jTextFieldTelephone)
                            .addComponent(jTextFieldNumClient, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                            .addComponent(jTextFieldPrenom, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonRechercheClient)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonMAJClient)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonNewClient))))
                    .addComponent(jTextFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(319, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                    .addComponent(jTextFieldNumClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonRechercheClient))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButtonMAJClient)
                    .addComponent(jButtonNewClient)
                    .addComponent(jTextFieldNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldPrenom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldTelephone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelValidation)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelPiece)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelRepresentation)
                .addGap(9, 9, 9)
                .addComponent(jLabelTarif)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelNbPlaces)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelTotal)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonValider)
                    .addComponent(jButtonAnnuler))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonValiderActionPerformed
        if(this.monClient!=null){
            for(int i=0;i<this.nbPlaceReserv;i++){
                new Reservation(this.monClient, this.monTarif, this.maRepresentation,this.monVendeur);
            }
            JOptionPane.showMessageDialog(this.maFrame, "Commande enregistrée","",JOptionPane.INFORMATION_MESSAGE);
            this.maFrame.chargerlesPieces();
        }else{
            JOptionPane.showMessageDialog(this.maFrame, "Client non renseigné","",JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_jButtonValiderActionPerformed

    private void jButtonRechercheClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRechercheClientActionPerformed
        monClient=Client.findEntitie(Integer.parseInt(this.jTextFieldNumClient.getText()));
        if(monClient==null){
            this.jTextFieldNom.setText("");
            this.jTextFieldPrenom.setText("");
            this.jTextFieldTelephone.setText("");
            this.jTextFieldEmail.setText("");
            JOptionPane.showMessageDialog(this.maFrame, "Client introuvable","",JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            this.jTextFieldNom.setText(monClient.getNom());
            this.jTextFieldPrenom.setText(monClient.getPrenom());
            this.jTextFieldTelephone.setText(monClient.getNumTelephone());
            this.jTextFieldEmail.setText(monClient.getEmail());
        }
}//GEN-LAST:event_jButtonRechercheClientActionPerformed

    private void jTextFieldTelephoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldTelephoneActionPerformed
}//GEN-LAST:event_jTextFieldTelephoneActionPerformed

    private void jButtonMAJClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMAJClientActionPerformed
        monClient.setNom(this.jTextFieldNom.getText());
        monClient.setPrenom(this.jTextFieldPrenom.getText());
        monClient.setTelephone(this.jTextFieldTelephone.getText());
        monClient.setEmail(this.jTextFieldEmail.getText());
        Client.edit(monClient);
        JOptionPane.showMessageDialog(this.maFrame, "Client mis à jour","",JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_jButtonMAJClientActionPerformed

    private void jButtonNewClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewClientActionPerformed
        monClient=new Client(this.jTextFieldNom.getText(),
                this.jTextFieldPrenom.getText(),
                null,null,
                this.jTextFieldTelephone.getText(),
                this.jTextFieldEmail.getText());
        JOptionPane.showMessageDialog(this.maFrame, "Client enregistré","",JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jButtonNewClientActionPerformed

    private void jButtonAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAnnulerActionPerformed
        this.maFrame.chargerlesPieces();
    }//GEN-LAST:event_jButtonAnnulerActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAnnuler;
    private javax.swing.JButton jButtonMAJClient;
    private javax.swing.JButton jButtonNewClient;
    private javax.swing.JButton jButtonRechercheClient;
    private javax.swing.JButton jButtonValider;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabelNbPlaces;
    private javax.swing.JLabel jLabelPiece;
    private javax.swing.JLabel jLabelRepresentation;
    private javax.swing.JLabel jLabelTarif;
    private javax.swing.JLabel jLabelTotal;
    private javax.swing.JLabel jLabelValidation;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextFieldEmail;
    private javax.swing.JTextField jTextFieldNom;
    private javax.swing.JTextField jTextFieldNumClient;
    private javax.swing.JTextField jTextFieldPrenom;
    private javax.swing.JTextField jTextFieldTelephone;
    // End of variables declaration//GEN-END:variables

}
